; deftemplejty : 
(deftemplate visible (slot Object) (slot PosX) (slot PosY)) 
(deftemplate field (slot Type) (slot Resource) (slot PosX) (slot PosY)) 
(deftemplate stat (slot Name) (slot Value))	
	;zwracane
(deftemplate move (slot Move) (slot MoveX) (slot MoveY))
	;lokalne
(deftemplate twojeJedzenie (slot Resource) (slot PosX) (slot PosY))
(deftemplate pozycjaAgenta (slot nazwaPola) (slot wartoscPola))
;-------------------------------------------JEDZENIE ----------------------------
 (defrule jedz
	(and
		(stat (Name food) (Value ?v))
		(test (< ?v 5) )
		(field (Type WORM) (Resource ?r) (PosX 0) (PosY 0) )
	)
	=>
	(assert (move (Move collectWorm) (MoveX 0) (MoveY 0) ) )
)

;jeśli jestem głodny i nie stoje na jedzeniu to szukam jedzenia
	
(defrule sprawdzGore
	(and
		(stat (Name food) (Value ?v))
		(test (< ?v 5) )
		(or
			(field (Type WORM) (Resource ?r) (PosX 0) (PosY 1) )
			(field (Type WORM) (Resource ?r) (PosX 0) (PosY 2) )
		)
	)	
	=>
	(assert (move (Move move) (MoveX 0) (MoveY 1) ) )
)

(defrule sprawdzDol
	(and
		(stat (Name food) (Value ?v))
		(test (< ?v 5) )
		(or
			(field (Type WORM) (Resource ?r) (PosX 0) (PosY -1) )
			(field (Type WORM) (Resource ?r) (PosX 0) (PosY -2) )
		)
	)	
	=>
	(assert (move (Move move) (MoveX 0) (MoveY -1) ) )
)

(defrule sprawdzLewo
	(and
		(stat (Name food) (Value ?v))
		(test (< ?v 5) )
		(or
			(field (Type WORM) (Resource ?r) (PosX -1) (PosY 0) )
			(field (Type WORM) (Resource ?r) (PosX -2) (PosY 0) )
		)
	)	
	=>
	(assert (move (Move move) (MoveX -1) (MoveY 0) ) )
)

(defrule sprawdzPrawo
	(and
		(stat (Name food) (Value ?v))
		(test (< ?v 5) )
		(or
			(field (Type WORM) (Resource ?r) (PosX 1) (PosY 0) )
			(field (Type WORM) (Resource ?r) (PosX 2) (PosY 0) )
		)
	)	
	=>
	(assert (move (Move move) (MoveX 1) (MoveY 0) ) )
)

(defrule sprawdzPraweGoraDol
	(and
		(stat (Name food) (Value ?v))
		(test (< ?v 5) )
		(or
			(field (Type WORM) (Resource ?r) (PosX 1) (PosY 1) )
			(field (Type WORM) (Resource ?r) (PosX 1) (PosY -1) )
		)
	)	
	=>
	(assert (move (Move move) (MoveX 1) (MoveY 0) ) )
)

(defrule sprawdzLeweGoraDol
	(and
		(stat (Name food) (Value ?v))
		(test (< ?v 5) )
		(or
			(field (Type WORM) (Resource ?r) (PosX -1) (PosY 1) )
			(field (Type WORM) (Resource ?r) (PosX -1) (PosY -1) )
		)
	)	
	=>
	(assert (move (Move move) (MoveX -1) (MoveY 0) ) )
)

;-----------------------------WALKI I UCIECZKI TAKA SYTUACJA--------------------------------------------

; walcz z drapieznikiem jak go zobaczysz ale nie masz za malo HP
(defrule walcz
	(visible (Object DRAPIEZNIK) (PosX ?x) (PosY ?y))
	=>
	(assert (move (Move kill) (MoveX ?x) (MoveY ?y)))
	(printout t ZABILEM_DRAPIEZNIKA_AAA crlf))
	
	
; nie ma drapieżnika, broń roślinożerną przed drapieżnikiem


	(defrule bron
	(visible (Object BYSTRETKA_ROSLINOZERNA) (PosX ?x) (PosY ?y))
	(not (exists (visible (Object DRAPIEZNIK) (PosX ?xL) (PosY ?yL))))
	=>
	
	(if (not (= ?x 0) ) then
	(bind ?newX (* -1 (/ ?x (abs ?x))))
	)
	(if (not (= ?y 0) ) then
	(bind ?newY (* -1 (/ ?y (abs ?y))))
	)
	(if (= ?x 0 ) then
		(bind ?newX 0)
	)
	(if (= ?y 0) then
		(bind ?newY 0)
	)
	(if (>= ?x ?y) then
		(bind ?newY 0 )
	)
	(if (< ?x ?y ) then
		(bind ?newX 0)
	)
	(assert (move (Move move) (MoveX ?newX) (MoveY ?newY)))
	(printout t BRONIE crlf))
	
	
; mądra bystretka ucieka kiedy ma malo życia a widzi drapieznika!

	
	(defrule ucieczka
	(visible (Object DRAPIEZNIK) (PosX ?x) (PosY ?y))
	(not (exists (visible (Object BYSTRETKA_ROSLINOZERNA) (PosX ?xD) (PosY ?yD))))
	(stat (Name hp) (Value ?v))
	(test (<= ?v 1))
	=>
	(if (not (= ?x 0) ) then
	(bind ?newX (* -1 (/ ?x (abs ?x))))
	)
	(if (not (= ?y 0) ) then
	(bind ?newY (* -1 (/ ?y (abs ?y))))
	)
	(if (= ?x 0 ) then
		(bind ?newX 0)
	)
	(if (= ?y 0) then
		(bind ?newY 0)
	)
	(if (>= ?x ?y) then
		(bind ?newY 0 )
	)
	(if (< ?x ?y ) then
		(bind ?newX 0)
	)
	(assert (move (Move move) (MoveX ?newX) (MoveY ?newY)))
	(printout t UCIEKAAM crlf))

